package org.example.leetCode;

/**
 * @author Administrator
 */
public class GetLeastNumbers {
    public static void main(String[] args) {
        int[] arr = {3,2,1};
        GetLeastNumbers getLeastNumbers = new GetLeastNumbers();
        getLeastNumbers.quickSort(arr, 0, arr.length - 1);
        int[] leastNumbers = getLeastNumbers.getLeastNumbers(arr, 2);

        for (int i=0;i< leastNumbers.length;i++){
            System.out.println(leastNumbers[i]);
        }
    }


        public int[] getLeastNumbers(int[] arr, int k) {
            quickSort(arr, 0, arr.length - 1);
            int[] res = new int[k];
            for (int i = 0; i < k; i++) {
                res[i] = arr[i];
            }
            return res;
        }

        private void quickSort(int[] arr, int left, int right) {
            if (left>right){
                return;
            }
            int min = left;
            int max = right;
            int temp = arr[left];

            while (min < max) {
                while (min < max && arr[max] >= temp) {
                    max--;
                }
                while (min < max && arr[min] <= temp) {
                    min++;
                }
                if (min < max) {
                    int exc = arr[min];
                    arr[min] = arr[max];
                    arr[max] = exc;
                }
            }
            arr[left] = arr[min];
            arr[min] = temp;

            quickSort(arr, left, min - 1);
            quickSort(arr, min + 1, right);
        }
}
